package com.dgshanger.sanhxiaofeisc.mvp.model

import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.entity.PictureEntity
import com.dgshanger.sanhxiaofeisc.entity.ShopEnterEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.ShopEnterContract
import com.xx.baseutilslibrary.network.entity.BaseResponseEntity
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import io.reactivex.Observable
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * Created by JacobHHH on 2018/4/20.
 */
class ShopEnterModel : ShopEnterContract.Model {

    override fun submitAudit(fileList: Map<String, File>?, map: MutableMap<String, String?>?, httpObserver: XxBaseHttpObserver<ShopEnterEntity>?) {
        //var imageUrls: HashMap<String,String?> = HashMap()
        val type = RequestBody.create(MultipartBody.FORM, "app_shop_join")
        val imageBody0 = RequestBody.create(MultipartBody.FORM, fileList!!["file0"])
        val image0 = MultipartBody.Part.createFormData("newfile", fileList!!["file0"]!!.name, imageBody0)

        AppApi.Api().uploadPicture(type, image0)
                .flatMap { pictureEntity ->
                    //imageUrls["image0"] = pictureEntity.data!!.file_path
                    map!!["shop_company_licence_img"] = pictureEntity.data!!.file_path //营业执照
                    val imageBody1 = RequestBody.create(MultipartBody.FORM, fileList!!["file1"])
                    val image1 = MultipartBody.Part.createFormData("newfile", fileList!!["file1"]!!.name, imageBody1)
                    AppApi.Api()
                            .uploadPicture(type, image1)
                }
                .flatMap { pictureEntity: BaseResponseEntity<PictureEntity> ->
                    //imageUrls["image1"] = pictureEntity.data!!.file_path
                    map!!["shop_corporate_face_img"] = pictureEntity.data!!.file_path//身份证正面
                    val imageBody2 = RequestBody.create(MultipartBody.FORM, fileList!!["file2"])
                    val image2 = MultipartBody.Part.createFormData("newfile", fileList!!["file2"]!!.name, imageBody2)
                    AppApi.Api()
                            .uploadPicture(type, image2)
                }
                .flatMap { pictureEntity: BaseResponseEntity<PictureEntity> ->
                    //imageUrls["image2"] = pictureEntity.data!!.file_path
                    map!!["shop_corporate_back_img"] = pictureEntity.data!!.file_path//身份证反面
                    if (fileList!!["file3"] != null) {
                    val imageBody3 = RequestBody.create(MultipartBody.FORM, fileList!!["file3"])
                    val image3 = MultipartBody.Part.createFormData("newfile", fileList!!["file3"]!!.name, imageBody3)
                    AppApi.Api()
                            .uploadPicture(type, image3)
                    } else {
                    //没有第四张,直接返回模拟数据
                     Observable.just(BaseResponseEntity<PictureEntity>("1",PictureEntity("")))
                    }
                }
                .flatMap { pictureEntity ->

                    //imageUrls["image3"] =pictureEntity.data!!.file_path
                    map!!["shop_attachment_img"] =pictureEntity.data!!.file_path//特殊附件

                    //所有上传完成
                    AppApi.Api()
                        .shopEnter(map)
                }
                .compose(RxHelper.io_main())
                .compose(RxHelper.start_finish(httpObserver))
                .subscribe(httpObserver)

    }
}